package onest.springboot.mybatis.demo.controller;

import onest.springboot.mybatis.demo.entity.User;
import onest.springboot.mybatis.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;



@RestController
@RequestMapping("user")
public class UserController {
	@Resource
	UserService userService;
	
	@RequestMapping("list")
	public String getAll(Model model){
		model.addAttribute("users", userService.getAll());
		return "list";
	}
	@RequestMapping(value="add",method= RequestMethod.GET)
	public String add(Model model) {
		User user=new User();
		model.addAttribute("user",user);
		return "add";
	}
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String add(User user) {
		userService.saveUser(user);
		return "redirect:list";
	}
	@RequestMapping("task")
	public void task(){
		userService.task();
	}
}
